package com.macro.mall.tiny.security.util;

import com.macro.mall.tiny.modules.ums.model.UmsAdmin;
import com.macro.mall.tiny.modules.ums.service.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 获取当前登录后台管理员工具类
 */
@Component
public class AdminHolder {
    
    @Autowired
    private UmsAdminService adminService;

    /**
     * 获取当前登录的管理员
     */
    public UmsAdmin getCurrentAdmin() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return adminService.getAdminByUsername(username);
    }

    /**
     * 获取当前登录管理员的用户名
     */
    public String getCurrentAdminUsername() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    /**
     * 获取当前登录管理员的ID
     */
    public Long getCurrentAdminId() {
        UmsAdmin admin = getCurrentAdmin();
        return admin != null ? admin.getId() : null;
    }

    /**
     * 判断当前是否有管理员登录
     */
    public boolean isAdmin() {
        return SecurityContextHolder.getContext().getAuthentication() != null 
            && SecurityContextHolder.getContext().getAuthentication().isAuthenticated();
    }
}
